package main

import "fmt"

type dbOptions struct {
	Host     string
	Port     int
	UserName string
	Password string
	DBName   string
}

type Option func(*dbOptions)

// WithHost 这个函数主要用来设置Host
func WithHost(host string) Option {
	return func(c *dbOptions) {
		c.Host = host
	}
}

func NewOpts(options ...Option) dbOptions {
	// 先实例化好 dbOptions, 填充上默认值
	dbopts := &dbOptions{
		Host: "127.0.0.1",
		Port: 3306,
	}

	for _, option := range options {
		option(dbopts)
	}
	return *dbopts
}

func main() {
	opts := NewOpts(WithHost("192.168.101.26"))
	fmt.Println(opts)
}
